package cn.edu.nuc.chat.frame;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.chat.server.TCPServer;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JTextField txtPort;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtPort = new JTextField();
		txtPort.setText("8000");
		txtPort.setBounds(196, 10, 91, 21);
		contentPane.add(txtPort);
		txtPort.setColumns(10);
		txtPort.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				JTextField txt = (JTextField)e.getSource();
				
			}
		});
		
		JButton btnNewButton = new JButton("启动监听服务");
		btnNewButton.setToolTipText("未启动");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JButton btn = ((JButton)e.getSource());
				if( "未启动".equals(btn.getToolTipText() )){
					TCPServer.start(Integer.valueOf(txtPort.getText().trim()));
					btn.setToolTipText("监听中");
					btn.setText("停止监听");
				} else {
					TCPServer.stop();
					btn.setToolTipText("未启动");
					btn.setText("启动监听服务");
				}
				
			}
		});
		btnNewButton.setBounds(297, 9, 127, 23);
		contentPane.add(btnNewButton);
		
		JLabel label = new JLabel("监听端口:");
		label.setBounds(120, 13, 66, 15);
		contentPane.add(label);
	}
}
